﻿using System.Linq;
using System.Net;
using System.Web.Mvc;
using System.Web.Routing;
using ResourcesOverMvc.Web.Mvc;
using Samples.WebService.Models;

namespace Samples.WebService.Controllers
{
    public class CategoriesController : Controller, IAllowedVerbs
    {
        public string GetAllowedVerbs(ActionExecutedContext actionExecutedContext) { return "GET"; }

        [HttpGet]
        public ActionResult Index()
        {
            var categories = Category.AllCategories;

            foreach (var category in categories)
            {
                category.Uri = Url.Action("Details", "Categories", new RouteValueDictionary { { "Id", category.CategoryId } }, Request.Url.Scheme, string.Empty);
            }

            return View(Category.AllCategories);
        }

        [HttpGet]
        public ActionResult Details(int id)
        {
            var category = Category.AllCategories.First(c => c.CategoryId == id);

            if (category == null)
            {
                return this.View("ErrorDetail", new ErrorDetail("The specified category does not exist."), HttpStatusCode.NotFound);
            }

            category.Uri = Url.Action("Details", "Categories", new RouteValueDictionary { { "Id", category.CategoryId } }, Request.Url.Scheme, string.Empty);

            return View(category);
        }
    }
}
